#### Fucking around

library('SDMTools')
library('raster')

grid.250m = read.asc('/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/DEM/ASCII/dem_WTplusbuffer_LatLong_WGS1984_250mres.asc')
solar.5k = read.asc.gz('/home1/99/jc152199/MicroclimateStatisticalDownscale/5kASCII/Solar/2006/20061231.asc.gz')

pos.5k = as.data.frame(which(is.finite(solar.5k), arr.ind=T))
pos.250m = as.data.frame(which(is.finite(grid.250m), arr.ind=T))

pos.5k$lat = getXYcoords(solar.5k)$x[pos.5k$row]
pos.5k$long = getXYcoords(solar.5k)$y[pos.5k$col]

pos.250m$lat = getXYcoords(grid.250m.nonas)$x[pos.250m$row]
pos.250m$long = getXYcoords(grid.250m.nonas)$y[pos.250m$col]

#### Trying get 'bounding box' lat long by loading a raster instead of an ASCII

raster.250m = raster(x='/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/DEM/Raster/Calculation2.img')
raster.5k = raster(x='/home1/99/jc152199/MicroclimateStatisticalDownscale/5kASCII/AnalysisMask5kResolution/Raster/projected.img')
raster.250m.nonas = raster.from.asc(grid.250m.nonas)
raster.solar.5k = raster.from.asc(solar.5k)

#### Because of the cellsize, raster.5k has a fractionally larger extent than raster.250m
#### Use the extent and cellsize to try and generate a 2-column matrix of row/columns positions, just like the base.pos command

extent.5k = c(xmin(raster.solar.5k),xmax(raster.solar.5k),ymin(raster.solar.5k),ymax(raster.solar.5k))
cellsize = .05

lat.pos = seq(extent.5k[1],extent.5k[2],cellsize)
long.pos = seq(extent.5k[3],extent.5k[4],cellsize)
all.pos = expand.grid(lat.pos,long.pos) # This is the raster equivalent of base.pos

# The follow commands work for a single cell in the 5k grid

extent1 = c(all.pos[1,1],all.pos[2,1],all.pos[52,2], all.pos[53,2]) # Create a vector of the extent of 1 5k grid, to feed into the cellsFromExtent function
	
	select.cells = cellsFromExtent(raster.250m.nonas,extent1) #### Returns the cell number for each 400 cells within the 5k grid
	
	checking2 = raster.250m.nonas[c(select.cells)]# This returns the VALUES from all 400 cells that fall within 1 grid of the 5k raster

	mean.value=mean(checking2,na.rm=T) # This command returns the mean value of the 400 cells

	checking2[which(is.finite(checking2))]=checking2[which(is.finite(checking2))]/mean.value # This command finds all finite values in the 400 cells and divides them by the mean value

	xy.5k = cbind(extent1[1]+.0025,extent1[3]+.0025) #### This extracts the appropriate value from the 5k grid, but will it fall down on the edges of the grid?
	value.5k = extract.data(xy.5k,solar.5k)

	checking2 = checking2*value.5k ### Now checking2 two represents the realized radiation values for the 400 cells within the one 5k grid

	checking2.mat = matrix(checking2,nrow=20, byrow=TRUE) ### This creates a matrix of 20 * 20 with values from checking2 in the appropriate order (I think)

	grid.250m.nonas[1:20,1:20]=checking2.mat    #### This inserts the values from checking2.mat back into grid.250m.nonas




